package br.com.jbssistemas.loterica.hibernate;

import java.sql.Connection;
import java.sql.SQLException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.hibernate.service.jdbc.connections.spi.ConnectionProvider;


/**
 *
 * @author daniel
 *
 */
public class HibernateUtil {

    private static SessionFactory sessionFactory;

    static {

        Configuration configuration = new Configuration();
        configuration.configure("br/com/jbssistemas/loterica/hibernate/hibernate.cfg.xml");
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
        sessionFactory = configuration.buildSessionFactory(serviceRegistry);
    
    }
    
    public static Session getSession(){
    
        return sessionFactory.openSession();
        
    }
    
    public static Connection getConnection() throws SQLException{
    
        SessionFactoryImplementor sfi = (SessionFactoryImplementor) HibernateUtil.getSession().getSessionFactory();
        
        ConnectionProvider connectionProvider = sfi.getConnectionProvider();
        
        Connection connection = connectionProvider.getConnection();
        
        return connection;
        
    }
    
}
